package com.wk1un2.controller;

import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wk1un2.service.RolesService;
import com.wk1un2.util.Result;

@RequestMapping("/public")
@RestController
public class RolesController {
	@Autowired
	private RolesService rolesservice;

	@RequestMapping("/getAllRoles")
	public Object getAll(@RequestParam Map<String, Object> map) {
		Integer page = Integer.parseInt(map.get("page").toString());
		Integer limit = Integer.parseInt(map.get("limit").toString());
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		Result res = new Result(0, "", rolesservice.count(map), rolesservice.getAllRoles(map));
		return res;
	}
	
	
	
	
	@RequestMapping("/addRoles")
	public Object add(@RequestParam Map<String, Object> map) {
			Result res = null;
			if (rolesservice.getRolesName(map.get("rolename").toString()) > 0) {
				res = new Result(0, "添加失败，角色名称不能重复!!!", -1, false);
			} else {
				res = new Result(0, "请求成功！！！", rolesservice.add(map), true);
			}
			return res;
	}

	@RequestMapping("/updateRoles")
	public Object update(@RequestParam Map<String, Object> map) {
		Result res = null;
		if (rolesservice.getRolesName(map.get("rolename").toString()) > 0) {
			res = new Result(0, "修改失败，角色名称不能重复!!!", -1, false);
		} else {
			res = new Result(0, "请求成功！！！", rolesservice.update(map), true);
		}
		return res;
	}

	@RequestMapping("/deleteRoles")
	public Object delete(Integer roleid) {
		Result res =null;
		if (rolesservice.getRolesUser(roleid)>0) {
			res = new Result(0, "删除失败，角色关联的有用户!!!", -1, false);
		} else {
			res = new Result(0, "请求成功", rolesservice.delete(roleid), true);
		}
		return res;
	}
	


	
	
	
	/**
	 * 添加角色菜单
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("/addrMenus")
	public Object addrMenus(@RequestParam Map<String, Object> map) {
		Result res = new Result(0, "请求成功", rolesservice.insertRoleModule(map), true);
		return res;
	}
	
	/**
	 * 删除角色模块
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("delrMenus")
	public Object delrMenus(@RequestParam Map<String, Object> map) {
		Result res = new Result(0, "请求成功", rolesservice.deleteRoleModule(map), true);
		return res;
	}
	
	
	

	
	@RequestMapping("/getAllBoxMenus")
	public Object getAllBoxMenus(@RequestParam Map<String, Object> map) {
		Result res = new Result(0, "true", 10, rolesservice.getAllBoxMenus(map));
		return res;
	}

	@RequestMapping("/getAllBoxRoleMenus")
	public Object getAllBoxRoleMenus(Integer roleid) {
		Result res = new Result(0, "true", rolesservice.getModules(roleid).size(), rolesservice.getModules(roleid));
		return res;
	}

	@RequestMapping("/addBoxMenus")
	public Object addBoxMenus(@RequestParam Map<String, Object> map) {
		int i = rolesservice.addBoxMenus(map);
		Result res = new Result(0, "true", i, i);
		return res;
	}

	@RequestMapping("/deleteBoxMenus")
	public Object deleteBoxMenus(@Param("roleid") Integer roleid,@Param("menuid") Integer menuid) {
		int i = rolesservice.deleteBoxMenus(roleid, menuid);
		Result res = new Result(0, "true", i, i);
		return res;
	}
	
	
	
	
	
	
}
